<template>
  <my-dialog
    v-model="dialogVisible"
    :title="title"
    :width="500"
    @close="close"
    @confirm="submit"
    @cancel="cancel"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="研究中心" prop="stationId">
        <select-form
          v-model="form.stationId"
          :options="centerOptions"
          :default-props="{ label: 'name', value: 'oid' }"
          :multiple="false"
        />
      </el-form-item>
      <el-form-item label="受试者识别码" prop="sic">
        <el-input v-model="form.sic" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio
            v-for="sex in dictStore.dictData[dictIdMaps.sex]"
            :label="sex.code"
            :key="sex.code"
            >{{ sex.value }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="form.birthday"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择"
          :disabled-date="disabledDate"
        />
      </el-form-item>
      <el-form-item label="入组日期" prop="inTime">
        <el-date-picker
          v-model="form.inTime"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择"
        />
      </el-form-item>
    </el-form>
  </my-dialog>
</template>

<script lang="ts" setup>
import { computed, defineEmits, defineProps, PropType, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'

import { ElMessage } from 'element-plus'

import MyDialog from '@/components/Dialog/index.vue'
import SelectForm from '@/components/Form/Select.vue'

import SubjectApi from '@/api/project/subject'
import { useDict } from '@/api/uses'

import { useDictStore } from '@/store/dict'

import IBaseObj from '#/index'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  centerOptions: {
    type: Array as PropType<IBaseObj[]>,
    default: () => [],
  },
})
const emits = defineEmits(['update:visible', 'search'])

const { dictIdMaps } = useDict()
const { getDictData } = useDictStore()
const dictStore = useDictStore()

getDictData()

const { pname, pid } = useRoute().query
const projectName = pname

const title = `项目：${projectName}`

const disabledDate = (time: any) => {
  return time.getTime() > Date.now()
}

const rules = {
  stationId: [{ required: true, message: '请选择研究中心', trigger: 'change' }],
  sic: [{ required: true, message: '请输入受试者识别码', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  birthday: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
  inTime: [{ required: true, message: '请选择入组日期', trigger: 'change' }],
}

const dialogVisible: any = computed({
  get() {
    return props.visible
  },
  set(val) {
    emits('update:visible', val)
  },
})

const form = reactive({
  stationId: '',
  sic: '',
  gender: '',
  birthday: '',
  inTime: '',
})

const formRef = ref()

const close = () => {
  formRef.value.resetFields()
}

const cancel = () => {
  dialogVisible.value = false
}

const submit = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const ret = await SubjectApi.createSubject({
        ...form,
        pid,
      })
      if (ret) {
        dialogVisible.value = false
        ElMessage({ type: 'success', message: '创建成功！' })
        emits('search')
      }
    }
  })
}
</script>
